package com.shell.customer.accreditation.rx;

import com.shell.bee.acct.AcctBlcService;
import com.shell.bee.acct.AcctService;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.constant.rel.AssetCode;
import com.shell.customer.accreditation.AccreditEvent;
import com.shell.customer.accreditation.vo.AccreditationVo;
import com.shell.customer.base.service.CusLvlService;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.invitation.service.InvtCusService;
import com.shell.customer.po.CusInvt;
import com.shell.customer.po.CusLvl;
import com.shell.customer.po.CusRec;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.CreditAssetKeyWord;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SendMsgForTbCusSubscriber extends OnErrorNotImplementedSubscriber<AccreditEvent> {
	@Autowired
	private CusLvlService cusLvlService;
	@Autowired
	private InvtCusService invtCusService;
	@Autowired
	private AcctService acctService;
	@Autowired
	private AcctBlcService acctBlcService;
	@Autowired
	private CusRecService cusRecService;

	public void onApplicationEvent(AccreditEvent event) {
		try {
			AccreditationVo accreditationVo = event.getAccreditationVo();
			CusRec cus = accreditationVo.getCustomerC();
			if (cus != null) {
				CusLvl cusLvl = new CusLvl();
				cusLvl.setCusId(cus.getCusId());
				cusLvl.setGrpId(cus.getGrpId());
				cusLvl.setCusLvl(Integer.valueOf(1));
				List<CusLvl> cusLvlList = this.cusLvlService.selectByEntity(cusLvl);
				if ((cusLvlList != null) && (cusLvlList.size() > 0)) {
					CusInvt cusInvt = new CusInvt();
					cusInvt.setInviteeId(cus.getCusId());
					List<CusInvt> invts = this.invtCusService.selectByEntity(cusInvt);
					String inviterId = ((CusInvt) invts.get(0)).getInviterId();
					AcctBas acctBas = this.acctService.getAcctBas(inviterId, "1", null);
					if (acctBas != null) {
						AcctBkp bkp = new AcctBkp();
						bkp.setAcctNo(acctBas.getAcctNo());
						bkp.setCurrCode(AssetCode.XTB.name());

						bkp = this.acctBlcService.getAcctBkp(bkp);
						if ((bkp != null) && (bkp.getAcctAmt().compareTo(BigDecimal.ZERO) > 0)
								&& (bkp.getAcctBlc().compareTo(BigDecimal.ZERO) > 0)) {
							CusRec cusRec = (CusRec) this.cusRecService.selectByKey(inviterId);
							CreditAssetKeyWord assetKeyWord = new CreditAssetKeyWord();
							assetKeyWord.setMemberName(cusRec.getCusName());
							assetKeyWord.setCreditAmt(bkp.getAcctAmt().toString());
							MessageSender.sendMessageByTmpl(cusRec.getContactorId(), "MT02623", assetKeyWord,
									cus.getCusId());
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
